<!DOCTYPE html>
<html>
<body>
<script src="/js-test-resources/js-test.js"></script>
<script>
description("The image bitmap factories should not throw exceptions on cross-origin access.");

window.jsTestIsAsync = true;
var reason;

function shouldBeAcceptedAndTainted(promise, message) {
    return promise.then(function(imageBitmap) {
        testPassed('Resolved as expected: ' + message);
        shouldBeTainted(imageBitmap);
    }, function(e) {
        reason = e;
        testFailed('Rejected unexpectedly: ' + message);
        shouldBeTrue('reason instanceof Error');
        debug(e);
    });
}

function shouldBeTainted(imageBitmap) {
    var canvas = document.createElement("canvas");
    canvas.width = 10;
    canvas.height = 10;
    var context = canvas.getContext("2d");
    context.drawImage(imageBitmap, 0, 0, 10, 10);
    try {
        var imageData = context.getImageData(0, 0, 10, 10);
        testFailed("ImageBitmap is not tainted.");
    } catch (e) {
        testPassed("ImageBitmap is tainted. Threw error: " + e);
    }
}

var image = document.createElement('img');
image.src = 'http://localhost:8080/security/resources/abe.png';
var video = document.createElement('video');
video.src = 'http://localhost:8080/media/resources/load-video.php?name=test.ogv&amp;type=video/ogv';

image.addEventListener('load', function() {
    document.body.appendChild(image);
    shouldBeAcceptedAndTainted(createImageBitmap(image, 0, 0, 10, 10), 'image')
.then(function() {
    var canvas = document.createElement("canvas");
    canvas.width = 10;
    canvas.height = 10;
    var context = canvas.getContext("2d");
    // taint the canvas
    context.drawImage(image, 0, 0, 10, 10);
    shouldBeAcceptedAndTainted(createImageBitmap(canvas, 0, 0, 10, 10), 'canvas')
.then(function() {
    createImageBitmap(image).then(imageBitmap => {
    shouldBeAcceptedAndTainted(createImageBitmap(imageBitmap, 0, 0, 10, 10), 'imageBitmap')
.then(function() {
    document.body.appendChild(video);
    video.play();
    video.addEventListener('playing', function() {
      shouldBeAcceptedAndTainted(createImageBitmap(video, 0, 0, 10, 10), 'video')
.then(finishJSTest, ()=> {
    testFailed("Unexpected failure");
    finishJSTest();
});
});
}, ()=> {
    testFailed("Unexpected failure");
    finishJSTest();
});
});
}, ()=> {
    testFailed("Unexpected failure");
    finishJSTest();
});
}, ()=> {
    testFailed("Unexpected failure");
    finishJSTest();
});
});
</script>
</body>
</html>
